package cn.edu.lingnan.servlet.project;

import cn.edu.lingnan.pojo.Project;
import cn.edu.lingnan.pojo.ProjectAdult;
import cn.edu.lingnan.pojo.User;
import cn.edu.lingnan.service.ProjectService;
import cn.edu.lingnan.service.ProjectServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet({"/deleteInsertProject","/deleteCreateProject"})
public class deleteInsertProjectServlet extends HttpServlet {
    private ProjectService projectService = new ProjectServiceImp();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String action = request.getServletPath();

        switch (action) {
            case "/deleteInsertProject":
                DeleteInsertProject(request, response);
                break;
            case "/deleteCreateProject":
                DeleteCreateProject(request, response);
                break;
        }
    }
    public void DeleteInsertProject(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer projectId=Integer.parseInt(  request.getParameter("projectId"));
       String studentName=   request.getParameter("studentName");
        Integer i = projectService.deleteInsertProject(projectId);
        HttpSession session = request.getSession();
        User user= (User) session.getAttribute("user");
        List<ProjectAdult> projectAdults = projectService.studentqueryaduitProject(user.getId());
        session.setAttribute("projectAdults", projectAdults);
        List<Project> noprojects = projectService.queryNoProjects(user.getName());
        request.getSession().setAttribute("noprojects", noprojects);
        response.sendRedirect(request.getContextPath() + "/sutdenr_Audits.jsp");

    }


    public void DeleteCreateProject(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer projectId=Integer.parseInt(  request.getParameter("projectId"));
        String studentName=   request.getParameter("studentName");
        System.out.println("删除项目的"+projectId+"学生"+studentName);
        Integer i = projectService.StudentDeleteProjectCreateAdultProject(projectId,studentName);

        HttpSession session = request.getSession();
        User user= (User) session.getAttribute("user");
        List<Project>  AdultProjects =  projectService.StudentQueryCreateAdultProject(user.getName());
        request.getSession().setAttribute("AdultProjects", AdultProjects);
        response.sendRedirect(request.getContextPath() + "/student_AdultProject.jsp");
    }
}